var app = getApp();
var that;
var problemDatas;
var utils = require('js/utils.js');
var userAnswers = [];
Page({
    data:{
        name:"",
        qos:"",
        total:31,
        dataItem:'',
        options:'',
        isSingle:'',
        isNoUserInput:true,
        userInfo: {},
        group:'',
        openid:'',
        basicNum:0,
        lifeNum:0,
        labelNum:0,
        commonNum:0,
        weakNum:0,
        userInputText:'',
        chooseType:1,
        chooseAmount:0
    },
    onLoad:function(){
        that = this;
        app.qosIndex = 0;//页面加载默认选择索引
        userAnswers = [];
        problemDatas = [];
        utils.userLogin(function(id,res){
                utils.getMyUnionid(res,function(unionid){
                    app.openId = unionid;
                    that.setData({
                        openid:unionid
                    });
                });
            });
        //调用应用实例的方法获取全局数据
        app.getUserInfo(function(userInfo){
            //更新数据
            that.setData({
                userInfo:userInfo,
                group:new Date().getTime(),
            })
            console.log(that.data.userInfo);  
        })
        utils.loadBasicDatas(function(isOk,data){
            if(isOk){
                console.log(that.data.basicNum);
                that.loadBasicOk(data);
            }else{
                console.log("loadBasicData fail");
            }
        })
    },
    //绑定用户输入
    bindKeyInput: function(e) {
        this.setData({
            userInputText: e.detail.value
        })
    },
    //用户选择答案点击
    userClick:function(e){
        var value = e.currentTarget.dataset.value;
        var index = value - 1;
        //基础筛选题目点击
        if(app.qosIndex <= that.data.basicNum){
            //用来处理单选
            that.dealChooseQos(index,function(){
                if(app.qosIndex < that.data.basicNum){
                    that.nextQos(problemDatas,app.qosIndex++);//下一题
                }else{
                    var param = JSON.stringify(userAnswers);
                    console.log(param);
                    //加载基础题目
                    utils.loadBaseData(param,that.data.openid,that.data.group,
                        function(isOk,data){
                            if(isOk){
                                that.loadBaseOk(data);
                            }else{
                                //that.loadBaseOk(app.baseData);//因为目前拿不到网络数据只能默认加载成功
                            }
                        }
                    );
                }
            });
        }
        //基础题目点击
        if(app.qosIndex>that.data.basicNum && app.qosIndex <= that.data.lifeNum){
            that.dealChooseQos(index,function(){
                if(app.qosIndex < that.data.lifeNum){
                    that.nextQos(problemDatas,app.qosIndex++);//下一题
                }else{
                    console.log(userAnswers);
                    var param = JSON.stringify(userAnswers);
                    //加载标签题目
                    utils.loadLabelData(param,that.data.openid,that.data.group,
                        function(isOk,data){
                            if(isOk){
                                that.loadLabelOk(data);
                            }else{

                            }
                        }
                    );
                }
            });
        }
        //标签题点击
        if(app.qosIndex>that.data.lifeNum&&app.qosIndex<=that.data.labelNum){
            that.dealChooseQos(index,function(){
                if(app.qosIndex < that.data.labelNum){
                    that.nextQos(problemDatas,app.qosIndex++);//下一题
                }else{
                    console.log('标签题目要传的答案');
                    console.log(userAnswers);
                    var param = JSON.stringify(userAnswers);
                    //加载普通题目
                    utils.loadCommonData(param,that.data.openid,that.data.group,
                        function(isOk,data){
                            if(isOk){
                                //加载普通题目完成
                                that.loadCommonDataOk(data);
                            }else{
                                
                            }
                        });
                }
            });
        }
        //普通题
        if(app.qosIndex>that.data.labelNum&&app.qosIndex<=that.data.commonNum){
            that.dealChooseQos(index,function(){
                if(app.qosIndex < that.data.commonNum){
                    that.nextQos(problemDatas,app.qosIndex++);//下一题
                }else{
                    var param = JSON.stringify(userAnswers);
                    //加载最弱学课题目
                    utils.loadWeak(param,that.data.openid,that.data.group,
                        function(isOk,data){
                            if(isOk){
                                that.loadWeakOk(data);
                            }else{
                                
                            }
                        }
                    );
                }
            });
        }
        //最弱学科
        if(app.qosIndex>that.data.commonNum&&app.qosIndex<=that.data.weakNum){
            that.dealChooseQos(index,function(){
                if(app.qosIndex < that.data.weakNum){
                    that.nextQos(problemDatas,app.qosIndex++);//下一题
                }else{
                    var param = JSON.stringify(userAnswers);
                    //加载最弱学课题目
                    utils.loadWeak(param,that.data.openid,that.data.group,
                        function(isOk,data){
                            if(isOk){
                            that.submitWeakOk(data);
                            }else{
                                
                            }
                        }
                    );
                }
            });
        }
    },
    //用于多选点击
    nextQosClick:function(){
        if(that.data.chooseType == 1){
          that.getUserChooseAns();
          if (that.data.chooseAmount<=0){
             utils.showToast('至少选择一个选项');
             return;
          }
        } else if (that.data.chooseType == 2){
          if (that.data.userInputText.length == 0){
            utils.showToast('请输入文本');
            return;
          }
        }
        //基础筛选题目点击
        if(app.qosIndex <= that.data.basicNum ){
            that.dealMultiChooseQos(function(){
                if(app.qosIndex < that.data.basicNum){
                    that.nextQos(problemDatas,app.qosIndex++);
                }else{
                    // console.log('标签题目要传的答案');
                    // console.log(userAnswers);
                    var param = JSON.stringify(userAnswers);
                    // console.log(param);
                    //加载基础题目
                    utils.loadBaseData(param,that.data.openid,that.data.group,
                        function(isOk,data){
                            if(isOk){
                                that.loadBaseOk(data);
                            }else{
                                //that.loadBaseOk(app.baseData);//因为目前拿不到网络数据只能默认加载成功
                            }
                        }
                    );
                }
            });
        }
        //生活题点击
        if(app.qosIndex>that.data.basicNum&&app.qosIndex<=that.data.lifeNum){
            that.dealMultiChooseQos(function(){
                if(app.qosIndex < that.data.lifeNum){
                    that.nextQos(problemDatas,app.qosIndex++);
                }else{
                    console.log(userAnswers);
                    var param = JSON.stringify(userAnswers);
                    ///加载标签题目
                    utils.loadLabelData(param,that.data.openid,that.data.group,
                        function(isOk,data){
                            if(isOk){
                                that.loadLabelOk(data);
                            }else{

                            }
                        }
                    );
                }
            });
        }
        //标签题点击
        if(app.qosIndex>that.data.lifeNum&&app.qosIndex<=that.data.labelNum){
            that.dealMultiChooseQos(function(){
                if(app.qosIndex < that.data.labelNum){
                    that.nextQos(problemDatas,app.qosIndex++);//下一题
                }else{
                    var param = JSON.stringify(userAnswers);
                    console.log("标签题的答案");
                    console.log(param);
                    //加载普通题目
                    utils.loadCommonData(param,that.data.openid,that.data.group,
                        function(isOk,data){
                            if(isOk){
                                //加载普通题目完成
                                that.loadCommonDataOk(data);
                            }else{
                                
                            }
                        });
                }
            });
        }
        //普通题
        if(app.qosIndex>that.data.labelNum&&app.qosIndex<=that.data.commonNum){
            that.dealMultiChooseQos(function(){
                if(app.qosIndex < that.data.commonNum){
                    that.nextQos(problemDatas,app.qosIndex++);//下一题
                }else{
                    var param = JSON.stringify(userAnswers);
                    //加载最弱学课题目
                    utils.loadWeak(param,that.data.openid,that.data.group,
                        function(isOk,data){
                            if(isOk){
                                that.loadWeakOk(data);
                            }else{
                                
                            }
                        }
                    );
                }
            });
        }
        //最弱学科
        if(app.qosIndex>that.data.commonNum&&app.qosIndex<=that.data.weakNum){
            that.dealMultiChooseQos(function(){
                if(app.qosIndex < that.data.weakNum){
                    that.nextQos(problemDatas,app.qosIndex++);//下一题
                }else{
                    var param = JSON.stringify(userAnswers);
                    //加载最弱学课题目
                    utils.loadWeak(param,that.data.openid,that.data.group,
                        function(isOk,data){
                            if(isOk){
                                that.submitWeakOk(data);
                            }else{
                                
                            }
                        }
                    );
                }
            });
        } 
    },
    /**
     * 从问题集合中拿数据
     * data 从网络访问拿到的数据
     * index 拿第几个问题
     */
    nextQos:function(data,index){
        that.data.dataItem = data[index];
        console.log('nextQos');
        console.log(data[index]);
        if(that.data.dataItem.is_checkbox == '0'){//单选 
            that.setData({
                isSingle:false,
                isNoUserInput:true
            });
        }else if(that.data.dataItem.is_checkbox == '1'){//多选 有下一题
            that.setData({
                isSingle:true,
                isNoUserInput:true,
                chooseType:1
            });
        }else if(that.data.dataItem.is_checkbox == '2'){//用户输入题
            that.setData({
                isSingle:true,
                isNoUserInput:false,
                chooseType: 2
            });
        }
        if(that.data.dataItem.is_checkbox == '0' || that.data.dataItem.is_checkbox == '1'){//单选或多选
            for(var i=0;i<that.data.dataItem.option.length;i++){
                that.data.dataItem.option[i].isChoosed = false;
            }
            that.setData({
                name:"问题"+(index+1),
                qos:that.data.dataItem.name,
                options:that.data.dataItem.option
            });
        }else if(that.data.dataItem.is_checkbox == '2'){//用户输入题
            that.setData({
                name:"问题"+(index+1),
                qos:that.data.dataItem.name,
            });
        }
    },
    //用于处理单选点击
    dealChooseQos:function(index,condition){
        console.log('用户进行了单选的点击');
        if(that.data.dataItem.is_checkbox == '0'){//单选
                that.data.dataItem.option[index].isChoosed = !that.data.dataItem.option[index].isChoosed;
                that.setData({
                    options:that.data.dataItem.option
                });
                userAnswers.push({"tid":problemDatas[app.qosIndex-1].id,"id":that.data.dataItem.option[index].id,"checkbox":"0"});
                console.log('单选');
                console.log(userAnswers);
                console.log(app.qosIndex);
                condition();
            }else if(that.data.dataItem.is_checkbox == '1'){//多选
                that.data.dataItem.option[index].isChoosed = !that.data.dataItem.option[index].isChoosed;
                that.setData({
                    options:that.data.dataItem.option
                });
            }
    },
    //用于处理多选题目
    dealMultiChooseQos:function(condition){
        var index = app.qosIndex - 1;//拿到当前页面的索引
        if(that.data.isNoUserInput){//选择题
            var ansIds = [];
            for(var i=0;i<that.data.options.length;i++){
                var op = that.data.options[i];
                if(op.isChoosed){
                    ansIds.push(op.id);
                }
            }
            userAnswers.push({"tid":problemDatas[app.qosIndex-1].id,"id":ansIds,"checkbox":"1"});//把多选题目放入
        }else{//
            userAnswers.push({"tid":problemDatas[app.qosIndex-1].id,"id":that.data.userInputText,"checkbox":"2"});//把用户输入的答案
        }
        condition();
    },
    getUserChooseAns:function(){
      var index = app.qosIndex - 1;//拿到当前页面的索引
      var count;//用于记录用户选择答案数量
      if (that.data.isNoUserInput) {//选择题
        count = 0;//给值初始化
        var ansIds = [];
        for (var i = 0; i < that.data.options.length; i++) {
          var op = that.data.options[i];
          if (op.isChoosed) {
            ansIds.push(op.id);
            console.log(op);
            count++;
          }
        }
        that.setData({
          chooseAmount: count
        })
      }
    },
    //加载基础筛选题目完成
    loadBasicOk:function(data){
        console.log('load basic ok');
        console.log(data);
        that.setData(
            {
                basicNum:data.data.result.number,
                lifeNum:parseInt(data.data.result.number)+1,
                labelNum:parseInt(data.data.result.number)+1+1,
                commonNum:parseInt(data.data.result.number)+1+1+1,
                weakNum:parseInt(data.data.result.number)+1+1+1+1
            }
        );
        problemDatas = data.data.result.question;
        that.nextQos(data.data.result.question,app.qosIndex++);
    },
    //加载基础筛选题目失败
    loadBasicFail:function(data){
        console.log(data);
        wx.showToast({
            title: '加载失败',
            icon: 'cancel',
            mask:true,
            duration: 2000
        });
    },
    //加载基础题目完成(生活)
    loadBaseOk:function(data){
        console.log("基础数据(生活题目)");
        var lifeData = data.data.result.question;
        console.log(lifeData);
        that.setData(
            {
                lifeNum:parseInt(data.data.result.number)+parseInt(that.data.basicNum),
                labelNum:parseInt(data.data.result.number)+parseInt(that.data.basicNum)+1,
                commonNum:parseInt(data.data.result.number)+parseInt(that.data.basicNum)+1+1,
                weakNum:parseInt(data.data.result.number)+parseInt(that.data.basicNum)+1+1+1
            }
        );
        console.log("lifeNum："+that.data.lifeNum);
        console.log('labelNum:'+that.data.labelNum);
        lifeData.forEach(function(e){
            problemDatas.push(e);//把获取的问题数据放入到数组中
        });
        that.nextQos(problemDatas,app.qosIndex++);//下一题
        userAnswers = [];
    },
    //加载基础题目失败
    loadBaseFail:function(data){
        wx.showToast({
            title: '加载失败',
            icon: 'cancel',
            mask:true,
            duration: 2000
        });
    },
    //加载标签题目完成
    loadLabelOk:function(data){
        console.log("标签数据");
        var labelData =  data.data.result.question;
        console.log(labelData);
        that.setData(
            {
                labelNum:parseInt(data.data.result.number)+parseInt(that.data.lifeNum),
                commonNum:parseInt(data.data.result.number)+parseInt(that.data.lifeNum)+1,
                weakNum:parseInt(data.data.result.number)+parseInt(that.data.lifeNum)+1+1
            }
        );
        labelData.forEach(function(e){
            problemDatas.push(e);//把获取的问题数据放入到数组中
        });
        that.nextQos(problemDatas,app.qosIndex++);//下一题
        userAnswers = [];
    },
    //加载标签题目失败
    loadLabelFail:function(){
        wx.showToast({
            title: '加载失败',
            icon: 'cancel',
            mask:true,
            duration: 2000
        });
    },
    //加载普通题目完成
    loadCommonDataOk:function(data){
        console.log("普通数据");
        var commonData =  data.data.result.question;
        console.log(commonData);
        that.setData(
            {
                commonNum:parseInt(data.data.result.number)+parseInt(that.data.labelNum),
                weakNum:parseInt(data.data.result.number)+parseInt(that.data.labelNum)+1
            }
        );
        commonData.forEach(function(e){
            problemDatas.push(e);//把获取的问题数据放入到数组中
        });
        that.nextQos(problemDatas,app.qosIndex++);//下一题
        userAnswers = [];
    },
    //加载普通题目失败
    loadCommonDataFail:function(data){
        wx.showToast({
            title: '加载失败',
            icon: 'cancel',
            mask:true,
            duration: 2000
        });
    },
    //加载学习题目完成
    loadWeakOk:function(data){
        console.log("学课题目");
        var weakData =  data.data.result.question;
        console.log(weakData);
        that.setData(
            {
                weakNum:parseInt(data.data.result.number)+parseInt(that.data.commonNum)
            }
        );
        weakData.forEach(function(e){
            problemDatas.push(e);//把获取的问题数据放入到数组中
        });
        that.nextQos(problemDatas,app.qosIndex++);//下一题
        userAnswers = [];
    },
    //加载学习题目失败
    loadWeakFail:function(data){
        wx.showToast({
            title: '加载失败',
            mask:true,
            icon: 'cancel',
            duration: 2000
        });
    },
    
    //提交最弱学科
    submitWeakOk:function(data){
        wx.navigateTo({
          url: '../report/report',
          success: function(res){
            // success
            console.log('success');
          },
          fail: function() {
            // fail
            console.log('fail');
          },
          complete: function() {
            // complete
            console.log('complete');
            app.qosIndex = 0;//页面加载默认选择索引
            userAnswers = [];
            problemDatas = [];
            utils.loadBasicDatas(function(isOk,data){
            if(isOk){
                console.log(that.data.basicNum);
                that.loadBasicOk(data);
            }else{
                console.log("loadBasicData fail");
                }
            })
          }
        })
    },
    submitWeakFail:function(data){
        wx.showToast({
            title: '加载失败',
            icon: 'success_no',
            mask:true,
            duration: 2000
        });
    },
    onShareAppMessage: function (res) {
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    return {
      title: '欢迎使用教带',
      path: 'pages/splash/splash',
      success: function(res) {
        // 转发成功
      },
      fail: function(res) {
        // 转发失败
      }
    }
  }
})